/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.bean;

import com.sjtu.constants.Constants;
import java.util.Date;
import com.sjtu.database.DataBase;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class UserBean {

    private int id;
    private String username;
    private String password;
    private String name;
    private String email;
    private String department;
    private int score;
    private int status;
    private Date createDate;

    private UserBean(int id, String username, String password, String name, String email, String department, int score, int status, Date createDate) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.email = email;
        this.department = department;
        this.score = score;
        this.status = status;
        this.createDate = createDate;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    private static UserBean setUser(ResultSet rs) {
        try {
            if (rs == null || !rs.next()) {
                return null;
            }
            return new UserBean(rs.getInt("id"), rs.getString("username"),
                    rs.getString("password"), rs.getString("name"),
                    rs.getString("email"),
                    rs.getString("department"), rs.getInt("score"),
                    rs.getInt("status"), rs.getDate("createDate"));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static UserBean getUser(String username, String password) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from user where username = '" + username + "' and password = '" + password + "';");
            UserBean user = setUser(rs);
            rs.close();
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static UserBean regist(String username, String password, String name,
            String email, String department, int score, int status) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select id from user where username = '" + username + "';");
            if (rs.next()) {
                return null;
            }
            Date dateNow = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createDate = dateFormat.format(dateNow);
            String sql = "insert into user (username,password,name,email,department," +
                    "score,status,createdate) values ('" + username + "','" + password + "','" + name + "','" +
                    email + "','" + department + "'," + score + "," + status + ",'" + createDate + "');";
//            System.out.println(sql);
            database.update("insert into user (username,password,name,email,department," +
                    "score,status,createdate) values ('" + username + "','" + password + "','" + name + "','" +
                    email + "','" + department + "'," + score + "," + status + ",'" + createDate + "');");
            rs = database.query("select * from user where username = '" + username + "';");
            UserBean user = setUser(rs);
//            System.out.println("id here is" + user.getId());
            rs.close();
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean addCategoryInterested(CategoryBean category) {
        DataBase database = DataBase.getInstance();
        String sql = "insert into usercategoryinfo (userid,categoryid) values (" + id + "," + category.getId() + ");";
        return database.update(sql);
    }

    public static boolean changePassword(int id, String password, String newPassword) {
        DataBase database = DataBase.getInstance();
        return database.update("update user set password = '" + newPassword + "' " +
                "where id = " + id + " and password = '" + password + "';");
    }

    public static boolean checkUser(int userId, int score) {
        DataBase database = DataBase.getInstance();
        return database.update("update user set score = " + score + ",status = " +
                Constants.UserStatus_Check + " where id = " + userId);
    }

    public static boolean rejectUser(int userId) {
        DataBase database = DataBase.getInstance();
        return database.update("update user set status = " +
                Constants.UserStatus_Reject + " where id = " + userId);
    }

    public boolean isAdmin() {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select id from admin where userId = " + id + ";");
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean userExisted(String userName) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select id from user where userName = '" + userName + "';");
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static int getUserId(String username) {
        try {
            DataBase database = DataBase.getInstance();
            System.out.println("select * from user where username = '" + username + "';");
            ResultSet rs = database.query("select * from user where username = '" + username + "';");
            return setUser(rs).getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static UserBean getUser(int userId) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from user where id = " + userId + ";");
            return setUser(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ArrayList<UserBean> getUserList() {
//        System.out.println("Here");
        ArrayList<UserBean> userToCheckList = new ArrayList<UserBean>();
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from user where status = " + Constants.UserStatus_Regist + ";");
            UserBean user;
            while ((user = setUser(rs)) != null) {
//                System.out.println(rs.getInt("id"));
                userToCheckList.add(user);
            }
            return userToCheckList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getAdminId(int userId) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select id from admin where userId = " + userId + ";");
            rs.next();
//                System.out.println(rs.getInt("id"));
            return rs.getInt("id");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
